"""
热点API数据模型
使用Pydantic实现完整的类型注解和验证
"""

from typing import List, Optional, Dict, Any
from pydantic import BaseModel, Field, validator
from datetime import datetime


class HotItem(BaseModel):
    """热点条目模型"""
    
    id: str | int = Field(..., description="条目唯一标识")
    title: str = Field(..., description="标题")
    description: Optional[str] = Field(None, description="描述/简介")
    cover: Optional[str] = Field(None, description="封面图片URL")
    author: Optional[str] = Field(None, description="作者/发布者")
    hot_value: Optional[int] = Field(None, description="热度值")
    timestamp: Optional[int] = Field(None, description="发布时间戳")
    url: str = Field(..., description="原始链接")
    mobile_url: str = Field(..., description="移动端链接")
    
    @validator('cover', 'url', 'mobile_url')
    def validate_urls(cls, v):
        """验证URL格式"""
        if v and not v.startswith(('http://', 'https://')):
            raise ValueError('URL必须以http://或https://开头')
        return v
    
    class Config:
        json_schema_extra = {
            "example": {
                "id": "123456",
                "title": "示例热点标题",
                "description": "这是一个示例热点的描述内容",
                "cover": "https://example.com/image.jpg",
                "author": "示例作者",
                "hot_value": 1000000,
                "timestamp": 1640995200,
                "url": "https://example.com/original",
                "mobile_url": "https://m.example.com/original"
            }
        }


class PlatformInfo(BaseModel):
    """平台信息模型"""
    
    name: str = Field(..., description="平台标识符")
    platform_name: str = Field(..., description="平台中文名称")
    type: str = Field(..., description="内容类型")
    description: Optional[str] = Field(None, description="平台描述")
    base_url: Optional[str] = Field(None, description="平台基础URL")
    available: bool = Field(True, description="平台是否可用")
    
    class Config:
        json_schema_extra = {
            "example": {
                "name": "zhihu",
                "platform_name": "知乎",
                "type": "热榜",
                "description": "知乎实时热榜",
                "base_url": "https://www.zhihu.com",
                "available": True
            }
        }


class HotResponse(BaseModel):
    """热点响应模型"""
    
    code: int = Field(200, description="响应状态码")
    platform: str = Field(..., description="平台标识符")
    platform_name: str = Field(..., description="平台中文名称")
    type: str = Field(..., description="内容类型")
    description: Optional[str] = Field(None, description="平台描述")
    total: int = Field(..., description="数据总数")
    update_time: str = Field(..., description="更新时间")
    from_cache: bool = Field(False, description="是否来自缓存")
    data: List[HotItem] = Field(..., description="热点数据列表")
    
    class Config:
        json_schema_extra = {
            "example": {
                "code": 200,
                "platform": "zhihu",
                "platform_name": "知乎",
                "type": "热榜",
                "description": "知乎实时热榜",
                "total": 50,
                "update_time": "2024-01-01 12:00:00",
                "from_cache": False,
                "data": [
                    {
                        "id": "123456",
                        "title": "示例热点",
                        "description": "描述内容",
                        "hot_value": 1000000,
                        "url": "https://example.com",
                        "mobile_url": "https://m.example.com"
                    }
                ]
            }
        }


class PlatformsResponse(BaseModel):
    """平台列表响应模型"""
    
    code: int = Field(200, description="响应状态码")
    count: int = Field(..., description="平台总数")
    platforms: List[PlatformInfo] = Field(..., description="可用平台列表")
    updated_at: datetime = Field(..., description="平台列表更新时间")
    
    class Config:
        json_schema_extra = {
            "example": {
                "code": 200,
                "count": 56,
                "platforms": [
                    {
                        "name": "zhihu",
                        "platform_name": "知乎",
                        "type": "热榜",
                        "available": True
                    },
                    {
                        "name": "bilibili",
                        "platform_name": "哔哩哔哩",
                        "type": "排行榜",
                        "available": True
                    }
                ]
            }
        }


class ErrorResponse(BaseModel):
    """错误响应模型"""
    
    code: int = Field(..., description="错误状态码")
    error: str = Field(..., description="错误类型")
    message: str = Field(..., description="错误信息")
    detail: Optional[str] = Field(None, description="详细错误信息")
    
    class Config:
        json_schema_extra = {
            "example": {
                "code": 404,
                "message": "平台不存在",
                "detail": "请求的平台 zhihu123 不存在"
            }
        }


class HotQueryParams(BaseModel):
    """热点查询参数模型"""
    
    limit: int = Field(50, ge=1, le=100, description="返回条目数量限制")
    use_cache: bool = Field(True, description="是否使用缓存")
    format: str = Field("json", pattern=r"^(json|rss)$", description="返回格式")
    
    class Config:
        json_schema_extra = {
            "example": {
                "limit": 20,
                "use_cache": True,
                "format": "json"
            }
        }


class CacheStats(BaseModel):
    """缓存统计模型"""
    
    platform: str = Field(..., description="平台名称")
    cache_hits: int = Field(0, description="缓存命中次数")
    cache_misses: int = Field(0, description="缓存未命中次数")
    last_update: Optional[str] = Field(None, description="最后更新时间")
    
    class Config:
        json_schema_extra = {
            "example": {
                "platform": "zhihu",
                "cache_hits": 150,
                "cache_misses": 25,
                "last_update": "2024-01-01 12:00:00"
            }
        }